package com.machine.action.user;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.machine.bean.User;
import com.machine.service.user.UserServiceDao;
import com.opensymphony.xwork2.ActionSupport;

@Component("userLogin")
@Scope(value="prototype")
public class UserLogin extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private String username;
	private String password;
	private UserServiceDao userServiceDao;

	
	
	public UserServiceDao getUserServiceDao() {
		return userServiceDao;
	}

	@Resource(name = "userService")
	public void setUserServiceDao(UserServiceDao userServiceDao) {
		this.userServiceDao = userServiceDao;
	}


	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	@Override
	public String execute() throws Exception {
		User user = userServiceDao.getUser(username, password);
		if(user != null){
			System.out.println("登录成功！");
		    ServletActionContext.getContext().getSession().put("username", username);
		    ServletActionContext.getContext().getSession().put("userid", user.getId());
			return SUCCESS;
		}else{
			System.out.println("登录失败！");
			return INPUT;
		}
	}
	
	

}
